package com.jams.goodox.activity.mine.personal;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.widget.EditTextWithDel;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jams.goodox.utils.Utils.isPhoneNum;

/**
 * Created by Jin on 2016/5/14.
 * Description 我的个人设置-修改手机号
 */
public class MinePersonalSettingsEditPhoneNumActivity extends BaseActivity {
    @Bind(R.id.inputBox)
    EditTextWithDel inputBox;
    @Bind(R.id.inputBox2)
    EditTextWithDel inputBox2;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.inputBox3)
    EditTextWithDel inputBox3;
    @Bind(R.id.ll5)
    TextView hintText;

    private static final int Time_Close = 60;//倒计时持续多少秒
    public static final String FromType = "FromType";
    private int fromType;//301：个人  302：企业
    @Override
    protected int getLayoutId() {
        return R.layout.personal_settings_phone_update;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "修改手机号");
        fromType = getIntent().getIntExtra(FromType,0);
        if(302==fromType)hintText.setText("手机号修改后将与企业账号绑定");//企业账号修改手机号，文案需修改
    }

    private void submit() {
        String oldPhone = inputBox.getText().toString().trim();
        final String newPhone = inputBox2.getText().toString().trim();
        String code = inputBox3.getText().toString().trim();

        if (TextUtils.isEmpty(oldPhone)) {
            ToastUtil.show("请输入旧手机号");
            return;
        }

        if (!isPhoneNum(oldPhone)) {
            ToastUtil.show("旧手机号错误");
            return;
        }

        if (TextUtils.isEmpty(newPhone)) {
            ToastUtil.show("请输入新手机号");
            return;
        }
        if (!isPhoneNum(newPhone)) {
            ToastUtil.show("新手机号错误");
            return;
        }

        if (TextUtils.isEmpty(code)) {
            ToastUtil.show("请输入验证码");
            return;
        }

        if (code.length() < 6) {
            ToastUtil.show("验证码错误");
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("newPhone", newPhone);
        params.put("oldPhone", oldPhone);
        params.put("verCode", code);
        params.put("type",fromType==301?"0":"1");//0:修改个人账号 1：修改企业账号
        showLoading();
        enqueue(createCallService(MineService.class).editUserPhone(params), new RetrofitCallbackListener<BaseEntity>() {
                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                        misLoading();
                        closeKeyboard();
                        ToastUtil.show( "手机号修改成功");
                        Intent i = new Intent();
                        i.putExtra("phone", newPhone);
                        setResult(RESULT_OK, i);
                        SePref.putUserPhone(newPhone);
                        finish();
                    }

                    @Override
                    public void onError(String msg) {
                        misLoading();
                        //ToastUtil.show(msg);
                    }
                }
        );

    }

    private void sendCode() {
        String newPhone = inputBox2.getText().toString().trim();
        if (TextUtils.isEmpty(newPhone)) {
            ToastUtil.showShort(mContext, "请输入新手机号");
            return;
        }
        if (newPhone.length() < 11) {
            ToastUtil.show("请输入正确的号码");
            return;
        }
        sendCode.setEnabled(false);
        Map<String,String> map=new HashMap<>();
        map.put("phone",newPhone);
        map.put("type","2");
        showLoading();
        enqueue(createCallService(LoginService.class).sendCode(map), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                sendCode.setEnabled(true);
                misLoading();
                Utils.countDown(Time_Close, sendCode, new Utils.BaseResult() {
                    @Override
                    public void onStart() {
                        sendCode.setBackgroundResource(R.drawable.bg_cancel_gray_deep);
                        sendCode.setEnabled(false);
                        sendCode.setTextColor(ContextCompat.getColor(MinePersonalSettingsEditPhoneNumActivity.this,R.color.white));
                    }

                    @Override
                    public void onResult(Boolean t) {
                        sendCode.setEnabled(true);
                        sendCode.setBackgroundResource(R.drawable.bg_cancel_theme);
                        sendCode.setText("获取验证码");
                        sendCode.setTextColor(ContextCompat.getColor(MinePersonalSettingsEditPhoneNumActivity.this,R.color.font_color_black));
                    }
                });
            }

            @Override
            public void onError(String msg) {
                sendCode.setEnabled(true);
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    @OnClick({R.id.send_code, R.id.save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.save:
                submit();
                break;
            case R.id.send_code:
                sendCode();
                break;
        }
    }

}

